﻿using D.B.Data.API.Applications.Command.LeaveCommand;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler.LeaveCommandHandler
{
    public class UpdateLeaveCommandHandler : IRequestHandler<UpdateLeaveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<WorkflowModel> baseRepository;

        public UpdateLeaveCommandHandler(IBaseRepository<WorkflowModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public Task<APIResult<int>> Handle(UpdateLeaveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = baseRepository.GetAll().FirstOrDefault(s => s.Id == request.Id);

            if(res.State==true)
            {
                result.Code = EnumCode.File;
                result.Msg = "已审批不能修改";
            }
            else
            {
                res.Desc=request.Desc;
                res.UpDataUserId=request.UserId;
                res.UpDataTime=DateTime.Now;
                res.TypeEnums=request.TypeEnums;
                res.LeaveTIme=Convert.ToDateTime(request.LeaveTIme);
                res.ReportTIme= Convert.ToDateTime(request.ReportTIme);
                res.TypeEnums = request.TypeEnums;
                baseRepository.Update(res);
                result.Code = EnumCode.Ok;
                result.Msg = "修改成功";
            }
            return Task.FromResult(result);
        }
    }
}
